﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using qlbansach_thuctapnhom.DAL;
using qlbansach_thuctapnhom.BLL;
namespace qlbansach_thuctapnhom.form
{
    public partial class Form_sach : Form
    {
        public Form_sach()
        {
            InitializeComponent();
            hienthi();
        }
        quanlybansachDataContext db = new quanlybansachDataContext();
        sach_bll s = new sach_bll();
        
        //=======================================================
        public void hienthi()
        {
            db = new quanlybansachDataContext();
            var hienthi = from ht in db.Saches select ht;
            dgv.DataSource = hienthi;
            dgv.DataBindings.Clear();
        }//======================================================
        private void btthem_Click(object sender, EventArgs e)
        {
            Sach obj = new Sach();
            bool ok = s.kiemtrasach(txtmasach.Text);
            if (ok)
            {
                obj.masach = txtmasach.Text;
                obj.ten = txttensach.Text;
                decimal giatien;
                giatien = decimal.Parse(txtgia.Text);

                obj.gia = giatien;
                obj.manxb = txtmanxb.Text;
                obj.matg = txtmatg.Text;
                obj.maloai = txtmaloai.Text;
                s.them(obj);
                lblerror.Text = "Thêm thành công";

            }
            else MessageBox.Show("Thêm thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            hienthi();

        }

        private void btnhaplai_Click(object sender, EventArgs e)
        {
            foreach (var c in groupPanel1.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
            }
        }

        private void btthoat_Click(object sender, EventArgs e)
        {
            formchinh f = new formchinh();
            f.Show();
            this.Close();
        }

        private void btsua_Click(object sender, EventArgs e)
        {
            bool ok = s.kiemtrasach(txtmasach.Text);
            if (!ok)
            {
                s.sua(txtmasach.Text, txttensach.Text,txtgia.Text,txtmanxb.Text,txtmatg.Text,txtmaloai.Text);

                lblerror.Text = "Sửa thành công";
            }
            else MessageBox.Show("Sửa thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            hienthi();
        }

        private void btxoa_Click(object sender, EventArgs e)
        {
            bool ok = s.kiemtrasach(txtmasach.Text);
            if (!ok)
            {
                s.xoa(txtmasach.Text);

                lblerror.Text = "Xóa thành công";


            }
            else MessageBox.Show("Không tìm thấy đối tượng cần xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            hienthi();
        }

        private void bttimasach_Click(object sender, EventArgs e)
        {
            bool ok = s.timkiemmasach(txtmasach.Text);
            if (ok)
            {
                dgv.DataSource = from ms in db.Saches where ms.masach.Equals(txtmasach.Text) select ms;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);


        }

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            int n = e.RowIndex;//n is the location of the selected line            
            txtmasach.Text = dgv.Rows[n].Cells[0].Value.ToString();
            txttensach.Text = dgv.Rows[n].Cells[1].Value.ToString();
            txtgia.Text = dgv.Rows[n].Cells[2].Value.ToString();
            txtmanxb.Text = dgv.Rows[n].Cells[3].Value.ToString();
            txtmatg.Text = dgv.Rows[n].Cells[4].Value.ToString();
            txtmaloai.Text = dgv.Rows[n].Cells[5].Value.ToString();
        }

        private void bttimtensach_Click(object sender, EventArgs e)
        {

            bool ok = s.timkiemtensach(txttensach.Text);
            if (ok)
            {
                dgv.DataSource = from ms in db.Saches where ms.ten.Equals(txttensach.Text) select ms;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void bttimgia_Click(object sender, EventArgs e)
        {

            bool ok = s.timkiemgia(txtgia.Text);
            if (ok)
            {
                dgv.DataSource = from ms in db.Saches where ms.gia.Equals(txtgia.Text) select ms;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void bttimnhaxb_Click(object sender, EventArgs e)
        {

            bool ok = s.timkiemmanxb(txtmanxb.Text);
            if (ok)
            {
                dgv.DataSource = from ms in db.Saches where ms.manxb.Equals(txtmanxb.Text) select ms;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void bttimtacgia_Click(object sender, EventArgs e)
        {

            bool ok = s.timkiemmatacgia(txtmatg.Text);
            if (ok)
            {
                dgv.DataSource = from ms in db.Saches where ms.matg.Equals(txtmatg.Text) select ms;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void bttimloaisach_Click(object sender, EventArgs e)
        {

            bool ok = s.timkiemmaloai(txtmaloai.Text);
            if (ok)
            {
                dgv.DataSource = from ms in db.Saches where ms.maloai.Equals(txtmaloai.Text) select ms;
                dgv.DataBindings.Clear();
                lblerror.Text = "Tìm thấy";
            }
            else MessageBox.Show("Không tìm thấy đối tượng ", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
    }
}
